/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package flames.view;

import com.sun.awt.AWTUtilities;
import flames.images.Colors;
import flames.utils.SwingUtils;
import javax.swing.*;
import java.awt.*;
import java.util.Objects;

/**
 * @author 10373
 */
public class PopConfirmDialog extends javax.swing.JDialog {

    public final Color ACTIVE_COLOR = new Color(242, 135, 24);    // #f28718
    private Point pressedPoint;
    public static final String ACCOUNT_IMG = "/flames/images/menu/bar_account.png";
    public static final String ACCOUNT_IMG_ACTIVE = "/flames/images/menu/bar_account_active.png";
    public static final String SETTING_IMG = "/flames/images/menu/bar_setting.png";
    public static final String SETTING_IMG_ACTIVE = "/flames/images/menu/bar_setting_active.png";
    public static final String UPDATE_IMG = "/flames/images/menu/bar_update.png";
    public static final String UPDATE_IMG_ACTIVE = "/flames/images/menu/bar_update_active.png";
    public static final String HELP_IMG = "/flames/images/menu/bar_help.png";
    public static final String HELP_IMG_ACTIVE = "/flames/images/menu/bar_help_active.png";
    public static final String ABOUT_IMG = "/flames/images/menu/bar_about.png";
    public static final String ABOUT_IMG_ACTIVE = "/flames/images/menu/bar_about_active.png";

    // 调用该提示框的父组件
    private Frame parent;

    /**
     * Creates new form MenuDialog
     */
    public PopConfirmDialog(java.awt.Frame parent, boolean modal) {
        this(parent, modal, null, 1);
    }

    /**
     * @param parent    在哪个组件上显示
     * @param modal     true:需要处理完这个弹框,才可以点击主页面 false:不需要
     * @param titleText 标题
     * @param flag      1:Success 0:Error 2:Warning
     */
    public PopConfirmDialog(java.awt.Frame parent, boolean modal, String titleText, int flag) {
        super(parent, modal);
        this.parent = parent;
        this.setUndecorated(true);
        initComponents();
        this.setLocationRelativeTo(parent);

        SwingUtils.beautifyButtonToTransparent(confirmBtn);
        SwingUtils.setBtnBackground(confirmBtn, Colors.ACTIVE_COLOR);
        if (titleText != null && !titleText.trim().equals("")) {
            title.setText(titleText);
        }
        if (flag == 1) {
            statusImg.setIcon(SwingUtils.buildImageIcon("/flames/images/status/Success.png"));
        } else if (flag == 2) {
            statusImg.setIcon(SwingUtils.buildImageIcon("/flames/images/status/Warning.png"));
        } else if (flag == 0) {
            statusImg.setIcon(SwingUtils.buildImageIcon("/flames/images/status/Error.png"));
        }

        AWTUtilities.setWindowOpacity(this, 0.99f);  // 设置透明度
        AWTUtilities.setWindowOpaque(this, false);  // 设置通透
    }


    // 切换按钮和图片样式
    private void toggleBtnStyleAndImgStyle(JButton button, JLabel buttonImg, String activeUrl, String normalUrl) {
        if (button.isContentAreaFilled()) {
            button.setContentAreaFilled(false);
            button.setForeground(Color.BLACK);
//            System.out.println("设置了Url:" + normalUrl);
            buttonImg.setIcon(new ImageIcon(Objects.requireNonNull(getClass().getResource(normalUrl))));
        } else {
            button.setContentAreaFilled(true);
            button.setBackground(ACTIVE_COLOR);
            button.setForeground(Color.WHITE);
//            System.out.println("设置了Url:" + activeUrl);
            buttonImg.setIcon(new ImageIcon(Objects.requireNonNull(getClass().getResource(activeUrl))));
        }
    }

    // 切换按钮文字样式
    private void toggleFontStyle(javax.swing.JButton button) {
        if (button.isContentAreaFilled()) {
            button.setForeground(Color.BLACK);
        } else {
            button.setForeground(Color.WHITE);
        }
    }

    // 切换图片
    private void toggleIcon(JButton button, JLabel buttonImg, String activeUrl, String normalUrl) {
        if (button.isContentAreaFilled()) {
//            System.out.println("设置了Url:" + normalUrl);
            buttonImg.setIcon(new ImageIcon(Objects.requireNonNull(getClass().getResource(normalUrl))));
        } else {
//            System.out.println("设置了Url:" + activeUrl);
            buttonImg.setIcon(new ImageIcon(Objects.requireNonNull(getClass().getResource(activeUrl))));
        }
    }

    // 美化按钮
    private void setBtnBeauty(JButton button) {
        button.setBorderPainted(false);
        button.setMargin(new Insets(0, 0, 0, 0));
        button.setContentAreaFilled(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        shadowLayeredPane = new DropShadowPanel(15);
        BKG = new javax.swing.JLabel();
        statusImg = new javax.swing.JLabel();
        title = new javax.swing.JLabel();
        confirmBtn = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(190, 250));
        addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                formMouseDragged(evt);
            }
        });
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                formMousePressed(evt);
            }
        });

        BKG.setIcon(new javax.swing.ImageIcon(getClass().getResource("/flames/images/SecurityToolsPane/Security_Tools_BKG.png"))); // NOI18N
        BKG.setToolTipText("");
        BKG.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                BKGMouseDragged(evt);
            }
        });
        BKG.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                BKGMousePressed(evt);
            }
        });

        statusImg.setIcon(new javax.swing.ImageIcon(getClass().getResource("/flames/images/status/Success.png"))); // NOI18N

        title.setFont(new java.awt.Font("微软雅黑", 0, 24)); // NOI18N
        title.setText("垃圾已全部清理完毕");

        confirmBtn.setFont(new java.awt.Font("微软雅黑", 1, 24)); // NOI18N
        confirmBtn.setForeground(new java.awt.Color(255, 255, 255));
        confirmBtn.setText("确定");
        confirmBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                confirmBtnActionPerformed(evt);
            }
        });

        shadowLayeredPane.setLayer(BKG, javax.swing.JLayeredPane.DEFAULT_LAYER);
        shadowLayeredPane.setLayer(statusImg, javax.swing.JLayeredPane.PALETTE_LAYER);
        shadowLayeredPane.setLayer(title, javax.swing.JLayeredPane.PALETTE_LAYER);
        shadowLayeredPane.setLayer(confirmBtn, javax.swing.JLayeredPane.PALETTE_LAYER);

        javax.swing.GroupLayout shadowLayeredPaneLayout = new javax.swing.GroupLayout(shadowLayeredPane);
        shadowLayeredPane.setLayout(shadowLayeredPaneLayout);
        shadowLayeredPaneLayout.setHorizontalGroup(
                shadowLayeredPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(shadowLayeredPaneLayout.createSequentialGroup()
                                .addGroup(shadowLayeredPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(shadowLayeredPaneLayout.createSequentialGroup()
                                                .addGap(250, 250, 250)
                                                .addComponent(confirmBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addComponent(BKG, javax.swing.GroupLayout.PREFERRED_SIZE, 390, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(0, 0, Short.MAX_VALUE))
                        .addGroup(shadowLayeredPaneLayout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(statusImg, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(title, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap())
        );
        shadowLayeredPaneLayout.setVerticalGroup(
                shadowLayeredPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(BKG, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(shadowLayeredPaneLayout.createSequentialGroup()
                                .addGap(22, 22, 22)
                                .addComponent(title, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(confirmBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(shadowLayeredPaneLayout.createSequentialGroup()
                                .addGap(45, 45, 45)
                                .addComponent(statusImg, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addComponent(shadowLayeredPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(61, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addComponent(shadowLayeredPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 57, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents


    // 使面板可被拖拽移动
    private void BKGMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_BKGMouseDragged
        formMouseDragged(evt);
    }//GEN-LAST:event_BKGMouseDragged

    // 使面板可被拖拽移动
    private void BKGMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_BKGMousePressed
        formMousePressed(evt);
    }//GEN-LAST:event_BKGMousePressed

    // 使面板可被拖拽移动
    private void formMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMousePressed
        pressedPoint = evt.getPoint();// 记录鼠标坐标
    }//GEN-LAST:event_formMousePressed

    // 使面板可被拖拽移动
    private void formMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseDragged
        Point point = evt.getPoint();// 获取当前坐标
        Point locationPoint = getLocation();// 获取窗体坐标
        int x = locationPoint.x + point.x - pressedPoint.x;// 计算移动后的新坐标
        int y = locationPoint.y + point.y - pressedPoint.y;
        setLocation(x, y);// 改变窗体位置,（按钮可以使用边界布局,也可以使用绝对布局）
    }//GEN-LAST:event_formMouseDragged

    // 提示框 点击[确定]之后的按钮, 将触发该方法
    private void confirmBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_confirmBtnActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_confirmBtnActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */

        //</editor-fold>
        //</editor-fold>

        /* Create and display the dialog */
        PopConfirmDialog.runInstance();
    }


    public static void runInstance() {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                // 新建[对话框]实例
                PopConfirmDialog dialog = new PopConfirmDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);    // 显示对话框
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel BKG;
    private javax.swing.JButton confirmBtn;
    private javax.swing.JLayeredPane shadowLayeredPane;
    private javax.swing.JLabel statusImg;
    private javax.swing.JLabel title;
    // End of variables declaration//GEN-END:variables
}
